"""帖子模型"""
# 字段类型引入 column列
from sqlalchemy import Column, Integer, DateTime, func, Text, ForeignKey, text
from soulapi.db import Base, engine
# relationship SQLAlchemy ORM提供的方法
from sqlalchemy.orm import relationship


# 基于BaseNoel定义请求体的结构（json对象）
class Topic(Base):
    __tablename__ = 'topic'  # 数据表表名，__tablename__：设置映射到的表名不设置，默认映射到类名post下
    __table_args__ = {'comment': '帖子'}
    top_id = Column(Integer, primary_key=True, index=True, autoincrement=True, server_default=text("0"), comment='帖子ID')  # index索引设置,autoincremen自增长
    top_content = Column(Text, nullable=False, comment="帖子内容")
    top_date = Column(DateTime, nullable=False, server_default=func.now(), comment="发帖时间")
    # 外键
    us_id = Column(Integer, ForeignKey('user.us_id'), comment="发帖人:用户")
    # 关系(back_populates双向显示)
    # user = relationship('User', back_populates='topic', foreign_keys=[us_id])
    #级联操作
    reply = relationship('Reply', back_populates='topic', cascade='all')
    # 时间获取
    created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')

# 在数据库中生成表
Base.metadata.create_all(engine)